package shg.book.loadImage
{
	
	import com.shuhg.bean.BookBean;
	import com.shuhg.bean.BookShow;
	import com.shuhg.book.util.BookTool;
	import com.shuhg.book.util.ReadBook;
	import com.shuhg.book.view.TextBook;
	import com.shuhg.book.write.index.ShowBox;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.NativeWindowSystemChrome;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.geom.Rectangle;
	import flash.net.FileFilter;
	import flash.net.URLRequest;
	import flash.system.Capabilities;
	import flash.utils.ByteArray;
	
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.Image;
	import mx.core.DragSource;
	import mx.managers.DragManager;
	import mx.managers.PopUpManager;
	
	import shg.book.view.MoveBook;

	/**
	 * 点击首页打开按钮加载书籍封面的方法类。
	 */ 
	public class LoadImage
	{
		public var flexBook:shuhg;
		public var booksArr:Array = new Array;
		private var fileFileter:FileFilter;
		private var file:File=File.documentsDirectory;
		private var loader:Loader;
		public var bitmap:Bitmap;
		public var isFull:Boolean = false;//书架是否已经摆满书籍，当书架摆满书籍时，无法再打开图片
		private var byArr:ByteArray;
		private var bookBean:BookBean;
		
		public function LoadImage()
		{
		}
		/**
		 * 点击打开按钮，上传图片
		 * */
		public function openImage(bookBean:BookBean):Number
		{
			this.bookBean=bookBean;
			getIsFull();
			if(!isFull){
				//书架未满，可以继续打开上传图片			
//				fileFileter=new FileFilter("选择文件图像*.jpg *.png *.gif","*.jpg;*.png;*.gif");
//				file.browseForOpen("打开图像",[fileFileter]);//默认为用户文档路径img
//				file.addEventListener(Event.SELECT,loaderImg);
				return getBmpInfo();
			}else{
				Alert.show("书架已经填满，请整理书架。");
			}
			return 0;
		}

		/**
		 * 判断书架是否已经摆满
		 * */
		private function getIsFull():void{
			var index:Number = getImgIndex();
			if(index == 0)
				//书架已经摆满
				isFull = true;
			else
				//书架未满
				isFull = false
		}
		/**
		 * 根据图像路径加载图像
		 * */
		private function loaderImg(event:Event):void{
			loader = new Loader();//Loader 类可用于加载 SWF 文件或图像（JPG、PNG 或 GIF）文件
			var file:File=event.target as File;
			var filePath:String=file.nativePath.toString();//获取选择文件的路径
			var request:URLRequest = new URLRequest(filePath);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,getBmpInfo);
			loader.load(request); //加载URLRequest指定的文件
		}
		/**
		 * 把图片存储在一个字节数组里面
		 * 
		 * */ 
		private function getBmpInfo(event:Event=null,bitmapByte:Bitmap=null):Number{
			//bitmap = (Bitmap)(loader.content);
			//this.bitmap=bitmapByte;
			byArr = bitmapTobyteArray(bookBean.coverBitmap);
			var tempBookImg:BookImage = createBook(byArr);
			var img:BookShow = new BookShow;
			img.bookBean = this.bookBean;
			img.maintainAspectRatio = false;
			img.toolTip = bookBean.bookName;
			img.percentHeight = 100;
			img.percentWidth = 100;
			img.source = byteArray2bitmap(tempBookImg.imageByteArr);//字节转换
			img.addEventListener(MouseEvent.DOUBLE_CLICK,clickImage);
			img.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
			var index:Number;
			if(isNaN(this.bookBean.bookIndex))
				index = getImgIndex();//获得当前图片应该存放的索引位置
			else
				index=this.bookBean.bookIndex;
			addImg2Canvas(index,img);//将图片添加到对应的canvas中
			return index;
		}
		/**
		 * 取得图片应该放置的书架位置
		 * 
		 * */ 
		private function getImgIndex():Number{
			if(flexBook.bookContain.c11.getChildren().length==0){
				return 1;
			}else if(flexBook.bookContain.c12.getChildren().length==0){
				return 2;
			}else if(flexBook.bookContain.c13.getChildren().length==0){
				return 3;
			}else if(flexBook.bookContain.c14.getChildren().length==0){
				return 4;
			}else if(flexBook.bookContain.c15.getChildren().length==0){
				return 5;
			}else if(flexBook.bookContain.c16.getChildren().length==0){
				return 6;
			}else if(flexBook.bookContain.c21.getChildren().length==0){
				return 7;
			}else if(flexBook.bookContain.c22.getChildren().length==0){
				return 8;
			}else if(flexBook.bookContain.c23.getChildren().length==0){
				return 9;
			}else if(flexBook.bookContain.c24.getChildren().length==0){
				return 10;
			}else if(flexBook.bookContain.c25.getChildren().length==0){
				return 11;
			}else if(flexBook.bookContain.c26.getChildren().length==0){
				return 12;
			}else if(flexBook.bookContain.c31.getChildren().length==0){
				return 13;
			}else if(flexBook.bookContain.c32.getChildren().length==0){
				return 14;
			}else if(flexBook.bookContain.c33.getChildren().length==0){
				return 15;
			}else if(flexBook.bookContain.c34.getChildren().length==0){
				return 16;
			}else if(flexBook.bookContain.c35.getChildren().length==0){
				return 17;
			}else if(flexBook.bookContain.c36.getChildren().length==0){
				return 18;
			}else if(flexBook.bookContain.c41.getChildren().length==0){
				return 19;
			}else if(flexBook.bookContain.c42.getChildren().length==0){
				return 20;
			}else if(flexBook.bookContain.c43.getChildren().length==0){
				return 21;
			}else if(flexBook.bookContain.c44.getChildren().length==0){
				return 22;
			}else if(flexBook.bookContain.c45.getChildren().length==0){
				return 23;
			}else if(flexBook.bookContain.c46.getChildren().length==0){
				return 24;
			}else{
				//返回0说明书架已经摆满，没有空余的书架容器了。此处用来判断书架是否摆满用
				return 1;
			}
		}
		/**
		 * 拖拽图片时调用的方法
		 * 
		 * */ 
		private function mouseMoveHandler(event:MouseEvent):void
		{
			var dragInitiator:BookShow=BookShow(event.currentTarget);
			var ds:DragSource = new DragSource();
			ds.addData(dragInitiator, "img");       
			var tempImage:BookShow = new BookShow;
			tempImage = dragInitiator;
			DragManager.doDrag(dragInitiator, ds, event,tempImage);
		}
		/**
		 * 把图片添加到书架对应的容器中
		 * 
		 * */ 
		private function addImg2Canvas(index:Number,img:BookShow):void{
			switch (index){
				case 1:
					addShow(flexBook.bookContain.c11,img);
					break;
				case 2:
					addShow(flexBook.bookContain.c12,img);
					break;
				case 3:
					addShow(flexBook.bookContain.c13,img);
					break;
				case 4:
					addShow(flexBook.bookContain.c14,img);
					break;
				case 5:
					addShow(flexBook.bookContain.c15,img);
					break;
				case 6:
					addShow(flexBook.bookContain.c16,img);
					break;
				case 7:
					addShow(flexBook.bookContain.c21,img);
					break;
				case 8:
					addShow(flexBook.bookContain.c22,img);
					break;
				case 9:
					addShow(flexBook.bookContain.c23,img);
					break;
				case 10:
					addShow(flexBook.bookContain.c24,img);
					break;
				case 11:
					addShow(flexBook.bookContain.c25,img);
					break;
				case 12:
					addShow(flexBook.bookContain.c26,img);
					break;
				case 13:
					addShow(flexBook.bookContain.c31,img);
					break;
				case 14:
					addShow(flexBook.bookContain.c32,img);
					break;
				case 15:
					addShow(flexBook.bookContain.c33,img);
					break;
				case 16:
					addShow(flexBook.bookContain.c34,img);
					break;
				case 17:
					addShow(flexBook.bookContain.c35,img);
					break;
				case 18:
					addShow(flexBook.bookContain.c36,img);
					break;
				case 19:
					addShow(flexBook.bookContain.c41,img);
					break;
				case 20:
					addShow(flexBook.bookContain.c42,img);
					break;
				case 21:
					addShow(flexBook.bookContain.c43,img);
					break;
				case 22:
					addShow(flexBook.bookContain.c44,img);
					break;
				case 23:
					addShow(flexBook.bookContain.c45,img);
					break;
				case 24:
					addShow(flexBook.bookContain.c46,img);
					break;
			}
		}
		/**
		 * 添加展开按钮
		 * */
		private function addShow(canve:Canvas,img:BookShow):void{
			canve.addChild(img);
			var _length:Number = canve.getChildren().length;
			if(_length==2){
				var btn:Button = new Button;
				btn.label="展开";
				btn.width = 30;
				btn.height = 30;
				BookTool.flexBook = this.flexBook;
				btn.addEventListener(MouseEvent.CLICK,BookTool.btnClick);
				canve.addChild(btn);
			}else if(_length>2){//将上一层的展开按钮放在最上层
				canve.addChildAt(canve.getChildAt(_length-2),_length-1);
			}
		}
		/**
		 * 打开书籍面板
		 * */ 
		private function clickImage(evt:MouseEvent):void{
			var readBook:ReadBook=new ReadBook();
			var bookShow:BookShow = evt.currentTarget as BookShow;
			readBook.filePath = bookShow.bookBean.filePath;
			readBook.selectFile();
		}
		/**
		 * 创建book类
		 * 
		 * */ 
		private function createBook(byArr:ByteArray):BookImage{
			var book:BookImage = new BookImage;
			book.imageByteArr = byArr;
			booksArr.push(book);
			return book;
		}
		/**
		 * 把Bitmap类型的数据转换为ByteArray类型的数据
		 * */
		private function bitmapTobyteArray(bitmap:Bitmap):ByteArray{
			var imageWidth:uint = bitmap.width;
			var imageHeigh:uint = bitmap.height;
			//创建一个具有指定高度和宽度的BitmapData对象
			var srcBmp:BitmapData = new BitmapData(imageWidth,imageHeigh);
			//或者指定路径的图片
			srcBmp.draw(bitmap);
			var bytes:ByteArray = srcBmp.getPixels( new Rectangle(0,0,imageWidth,imageHeigh) );    
			//下面俩行将数据源的高和宽一起存储到数组中,为翻转的时候提供高度和宽度     
			bytes.writeShort(imageHeigh);    
			bytes.writeShort(imageWidth);  
			return bytes;
		}
		/**
		 * 将bytearray转换成bitmap方法
		 * 
		 * */ 
		private function byteArray2bitmap(bytes:ByteArray):Bitmap{
			bytes.position=bytes.length-2; 
			var width:int = bytes.readShort();
			bytes.position=bytes.length-4;
			var height:int = bytes.readShort();
			var copyBmp:BitmapData = new BitmapData( width, height, true );    
			bytes.position = 0;    
			for( var i:uint=0; i<height ; i++ )    
			{    
				for( var j:uint=0; j<width; j++ )    
				{    
					copyBmp.setPixel( j, i, bytes.readUnsignedInt() );    
				}    
			}    
			var bmp:Bitmap = new Bitmap( copyBmp );    
			return bmp; 
		}
	}
}